package com.muqi.app.qlearn.mine;

import java.io.InputStream;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewTreeObserver;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout.LayoutParams;

import com.muqi.app.project.utils.BitmapUtils;
import com.muqi.app.project.utils.LogUtil;
import com.muqi.app.qlearn.student.BaseFragmentActivity;
import com.muqi.app.qlearn.student.R;
import com.muqi.app.user.widget.TreeView;

public class TreeActivity extends BaseFragmentActivity implements OnClickListener {
	private TreeView mTree;
	private AbsoluteLayout mTreeRl;
	private AbsoluteLayout.LayoutParams params;

//	int[] locationX = new int[] { 0, 29, 74, 65, 118, 126, 153, 124, 152, 177 };
//	int[] locationY = new int[] { 514, 559, 513, 590, 528, 562, 578, 632, 555,531 };
	
	int[] locationX = new int[] { 234, 415, 247, 478, 207, 333, 253, 509, 274, 204,443,418,398,284,475,290,467,227,480,293,184,
			454,391,309,237,396,279,442,435,512,
			179,438,499,224,512,208,464,195,474,399,304,238,287,442,
			232,319,151,363,175,453,498,394,248,478,279,278,406,
			215,318,157,446,546,193,534,
			149,519,587,138,405,214,512,177,524,266,254,445,521,495,
			117,116,560,211,265,547,372,109,495,152,563,304,481,152,
			516,148,307,551,186,153,275,555,137,559,524,448,183,424,
			118,605,585,301,102,289,560,177,595,316,51,523,600,178,
			282,642, 79,126,594,168,370,440,107,603,205,211,414,217,
			594,136,613,176,267,630,72,56,591,262,479,409,74,402,
			  0,161,539,256,656,479,226,
			 124,225,90,643,55,14,94,622,661,279,98,351,369,350,
			 643, 22, 29,257,500, 13,640, 66,608,395,293, 34,
			 65,9,154,316,678,280,127,74,106,451,107,323,621,
			 199,341,519,302,27,652,28,577,244,358,425,304};
	int[] locationY = new int[] { 594, 321, 353, 600, 485, 510, 646, 559, 638, 429,507,650,420,653,377,441,659,505,462,421,410,
			623,316,508,519,549,608,599,418,499,
			481,354,517,328,420,566,335,332,497,462,458,532,396,310,
			649,430,483,234,373,485,657,510,298,338,370,332,292,
			622,248,406,271,484,308,611,
			515,359,530,386,257,297,612,531,566,275,536,446,262,250,
			390,334,446,646,230,567,204,357,467,299,374,298,300,555,
			226,270,160,601,645,578,118,261,235,470,190,179,254,146,
			528,563,564,187,300,134,212,620,595,124,304,160,277,153,
			198,211,331,562,324,231,116,219,500,497,178,260,178,219,
			463,522,205,122,152,448,430,381,427,93,138,202,513,149,
			514,644,122,185,272, 84, 91,
			632,43,380,576,576,377,238,323,545,27,202,156,61,49,
			484,455,559, 53, 74,539,321,208,366, 91, 54,277,
			590,480,106,63,452,94,109,442,602,71,145,14,411,
			70,19,99,206,335,382,413,166,29,141,77,0};

	private AssetManager manager;
	private boolean hasMeasured = false;

	private int leafNum = 0;
	private double radioX;
	private double radioY;
	int height;
	int width;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tree);
		int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1048576);  
		LogUtil.e("Max memory is " + maxMemory + "MB"); 
	}

	@SuppressWarnings("deprecation")
	@Override
	protected void onInit() {
		leafNum = 105;
		mTreeRl = (AbsoluteLayout) findViewById(R.id.tree_rl);
		
		manager = getAssets();

		ViewTreeObserver vto = mTreeRl.getViewTreeObserver();

		vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

			@Override
			public boolean onPreDraw() {
// 				只需要加载一次即可
				if (!hasMeasured) {
					height = mTreeRl.getMeasuredHeight();
					width = mTreeRl.getMeasuredWidth();

					radioX = width * 1.0 / 700;
					radioY = height * 1.0 / 832;
//					控件的位置，暂时不需要
//					int x2 = (int) mTreeRl.getX();
//					int y2 = (int) mTreeRl.getY();

					// 获取到宽度和高度后，可用于计算
					LogUtil.e("控件的宽高为：" + width + "................" + height);
//					LogUtil.e("控件的位置为：" + x2 + "................" + y2);
					LogUtil.e("控件的比率为：" + radioX + "................" + radioY);

					for (int i = 0; i < leafNum; i++) {
						
						addLeafWithoutAnimation(i+1);
					}
					hasMeasured = true;
				}

				return true;
			}
		});
	}

	@Override
	protected void onData() {
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
	
		default:
			break;
		}
	}

	@SuppressWarnings("deprecation")
	private void addLeafWithAnimation(int num) {
		ImageView imageView = addLeaf(num);
		
		ObjectAnimator scaleY = ObjectAnimator.ofFloat(imageView, "scaleY", 1f, 2f, 1f);
		ObjectAnimator scaleX = ObjectAnimator.ofFloat(imageView, "scaleX", 1f, 2f, 1f);
		AnimatorSet animSet = new AnimatorSet();  
		animSet.play(scaleX).with(scaleY);  
		animSet.setDuration(1500);  
		animSet.start(); 
		
	}
	@SuppressWarnings("deprecation")
	private void addLeafWithoutAnimation(int num) {
		
		addLeaf(num);
	}
	
	Bitmap bitmap = null;
	
	@SuppressWarnings("deprecation")
	private ImageView addLeaf(int num) {
		
		
		try {
			InputStream in = manager.open("treepic/" + num + ".png");
			bitmap = BitmapFactory.decodeStream(in);
			bitmap = BitmapUtils.scaleImage(bitmap,((int) (bitmap.getWidth() * radioX)),((int) (bitmap.getHeight() * radioY)));

		} catch (Exception e) {
			LogUtil.e("出现异常，未加载到资源图片");
		}

		ImageView imageView = new ImageView(TreeActivity.this);
		if (bitmap != null) {
			LogUtil.e("bitmap生成了，，，，，，，，，，，"+num);
			imageView.setImageBitmap(bitmap);
		}

		params = new AbsoluteLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,(int) (locationX[num-1] * radioX),(int) (locationY[num-1] * radioY));
												
		imageView.setLayoutParams(params);

		mTreeRl.addView(imageView, params);
		
//		  if (bitmap != null && !bitmap.isRecycled()){  
//			  bitmap.recycle();  
//			  bitmap = null;  
//		  }
		return imageView;
		
	}
	public void onBack(View v){
		this.finish();
	}

}
